<%--
  01_jspExample.jsp
  User: PING
  Date: 2022/9/7
  Time: 9:50
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"
        import="com.javaweb.entity.Teacher"
        import="com.javaweb.common.DBUtil"
         import="java.util.*"
         import="com.javaweb.common.StringTool"
         import="com.javaweb.entity.Comment"
%>
<%@ page import="com.javaweb.entity.User" %>
<%@ page import="com.javaweb.common.NullableTool" %>
<%@ page import="java.util.concurrent.ConcurrentHashMap" %>
<html>
<meta charset="UTF-8">
<head>
    <title>JWP: Java Web 程序设计</title>
    <link rel="icon" href="../images/favico.jpg" type="image/x-icon" />
    <link rel="stylesheet" href="../css/main.css"/>
    <link rel="stylesheet" href="../css/show.css"/>
    <script src="../js/jquery-3.5.1.min.js"></script>
    <script src="../js/show.js"></script>
</head>
<body>

<%
    // block logic for invalided user
    User user = (User) session.getAttribute("user") ;
    if (user == null) {
        response.sendRedirect("login.jsp");
        return ;
    }
    /**
     * processing only for valided user
     */
    String msg = request.getParameter("msg");
    msg = msg == null ? "" : StringTool.URLDecodeInUTF(msg) ;
    List<Teacher> teacherList = (List<Teacher>) session.getAttribute("teacherList");
    List<Comment> comments = null ;
    Teacher teacher = null ;

    Map<Integer, Integer> favMap = (ConcurrentHashMap<Integer, Integer>) application.getAttribute("favMap") ;
    Map<Integer, Integer> unfavMap = (ConcurrentHashMap<Integer, Integer>)  application.getAttribute("unfavMap") ;
    Map<Integer, Integer> voteMap = (ConcurrentHashMap<Integer, Integer>)  application.getAttribute("voteMap") ;
    Map<Integer, List<Comment>> comMap = (ConcurrentHashMap<Integer, List<Comment>>)  application.getAttribute("comMap") ;
    int favNum, unfavNum, voteNum, comNum ;
%>
    <%@include file="header.jsp"%>
    <div class="container">
        <hr>
        <div class="main">
            <div class="show">
                    <div class="nav">
                        <div class="nav-menu">
                            <ul class="nav-list">
                                <li class="nav-list-tab"><a href="javascript:;">菜单1</a></li>
                                <li class="nav-list-tab"><a href="javascript:;">菜单2</a></li>
                                <li class="nav-list-logout"><a href="logoutProcess.jsp">登出</a></li>
                            </ul>

                        </div>
                        <div class="nav-user">
                            欢迎你，<a href="javascript:;"><%=user.getName()%></a>
                        </div>

                    </div>
                    <div class="list-head">
                        <p>候选教师名单</p>
                        <span class="show-msg"><%=msg%></span>
                    </div>
                <% for (int i=0; i<teacherList.size(); i++) {
                    teacher = teacherList.get(i) ;
                    comments = DBUtil.getCommentsForTeacher(teacher.getId()) ;
                    favNum = favMap.get(teacher.getId()) == null ? 0 : favMap.get(teacher.getId()) ;
                    unfavNum = unfavMap.get(teacher.getId()) == null ? 0 : unfavMap.get(teacher.getId()) ;
                    voteNum = voteMap.get(teacher.getId()) ;
                    comNum = comMap.get(teacher.getId()) == null ? 0 : comMap.get(teacher.getId()).size() ;

                %>
                    <div class="list-item">
                        <div class="item-img">
                            <img src="../images/<%=teacher.getImgURL()%>" alt=""/>
                        </div>
                        <div class="item-info">
                            <span class="item-name"><%=teacher.getName()%></span>
                            <span class="item-gender"><%=teacher.getGender()%></span>
                            <div class="item-desc">
                                    <p><%=teacher.getDesc()%></p>
                            </div>
                            <div class="item-comment">
                                <div class="item-comment-fav">
                                <i></i>
                                <a href="commentProcess.jsp?tId=<%=teacher.getId()%>&type=like">喜爱(<%=favNum%>)</a>
                                </div>
                                <div class="item-comment-unfav">
                                    <i></i>
                                    <a href="commentProcess.jsp?tId=<%=teacher.getId()%>&type=nolike">不喜欢(<%=unfavNum%>)</a>
                                </div>
                                <div class="item-comment-com">
                                    <i></i>
                                    <a class="comment" href="javascript:;">评论(<%=comNum%>)</a>
                                </div>
                            </div>
                        </div>
                        <div class="item-vote">
                            <h3>得票数：</h3><span class="item-num"><%=voteNum%></span>
                            <button class="item-vote-btn"><%=voteNum%></button>
                        </div>
                        <div class="item-action">
                            <a class="action-vote-btn" href="voteProcess.jsp?tId=<%=teacher.getId()%>&uId=<%=user.getId()%>">投票</a>
                        </div>
                        <div class="item-comment-body">
                            <div class="item-comment-header">
                                好评度<span class="item-comment-grade"><%=(favNum==0 && unfavNum == 0) ? 0.0 : StringTool.formatDouble(favNum * 100.0 / (favNum + unfavNum))%>%</span>
                            </div>
                            <%
                                if (comNum != 0) {
                                    for (int j=0; j<comments.size(); j++) {
                            %>
                            <div class="item-comment-item">
                                <div class="item-comment-author">
                                     <%= DBUtil.getUserNameById(comments.get(j).getUserId())%>&nbsp;说：
                                </div>
                                <div class="item-comment-content">
                                    <p><%=comments.get(j).getComment()%></p>
                                    <div class="item-comment-time"><%=StringTool.formatTime(comments.get(j).getCommentTime())%></div>
                                </div>
                            </div>
                            <%}
                                } else {
                            %>
                            <div class="item-comment-item">
                                <div class="item-comment-author">
                                </div>
                                <div class="item-comment-content">
                                    <p>暂无评论</p>
                                </div>
                            </div>
                            <%}
                            %>
                            <div class="item-comment-add">
                                <div class="item-comment-input">
                                    <form action="commentProcess.jsp?tId=<%=teacher.getId()%>&uId=<%=user.getId()%>" method="POST">
                                        <input type="text" name="newComment" placeholder="please input your comment"/>
                                        <input type="submit" value="评&nbsp;价"/>
                                    </form>
                                </div>
                            </div>


                        </div>
                    </div>
                <%}%>

            </div>
        </div>
    </div>

    <%@include file="footer.jsp"%>

</body>
</html>
